package com.imist.cake.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 处理登录和未登录的情况
 */
public class UserFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        String path = ((HttpServletRequest)request).getServletPath();
        if ("/login.do".equals(path)|| "/loginPrompt.do".equals(path)){
            filterChain.doFilter(request,response);
        } else if (null != (((HttpServletRequest) request).getSession().getAttribute("username"))){
            filterChain.doFilter(request,response);
        }else {
            request.getRequestDispatcher("loginPrompt.do").forward(request,response);
        }
    }

    public void destroy() {

    }
}
